<?php
/**
 * @name SampleModel
 * @desc sample数据获取类, 可以访问数据库，文件，其它系统等
 * @author root
 */
class UserModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
        $this->time = date("Y-m-d H:i:s",time());
    }

    public function userLogin($username,$password)
    {
        $sql = "select * from user where username='{$username}' and password='{$password}'";
        return $this->db->query($sql);
    }

    public function getUserInfo()
    {
        $sql = "select * from `user` ";
        return $this->db->query($sql);
    }
    public function selectUserID($username)
    {
        $sql="select user_id from user where username='{$username}'";
        return $this->db->query($sql);
    }
    public function selectUser($start,$everyPage)
    {
        $sql = "select * from `user` limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }
    public function getAllCount()
    {
        $sql="select count(*) as `rows` from `user` ";
        return $this->db->query($sql);
    }

    public function del($id)
    {
        $sql = "delete from user where user_id='{$id}' ";
        return $this->db->query($sql);
    }

    public function edit($info, $id)
    {
        //var_dump($info);
        $password = md5($info['password2']);
        $sql = "update `user` set `group_id`='{$info['group_id']}', `username`='{$info['name']}', `realname`='{$info['realname']}', `password`='{$password}',`create_time`=UNIX_TIMESTAMP('{$this->time}') WHERE `user_id`='{$id}'";
        return $this->db->query($sql);
    }

    public function isUserNameExistById($username, $id)
    {
        $sql = "select * from `user` where `username`='{$username}' and `user_id`!={$id} ";
        return $this->db->query($sql);
    }

    public function isUserNameExist($username)
    {
        $sql = "select * from `user` where  `username`='{$username}'";
        return $this->db->query($sql);
    }

    public function add($info)
    {
        $password = md5($info['password']);
        $name=$info['group_id'];
        $sql="insert into user(`password`,`realname`,`username`,`group_id`,`create_time`) values('{$password}','{$info['realname']}','{$info["name"]}','{$info['group_id']}',UNIX_TIMESTAMP('{$this->time}'))";
        return $this->db->query($sql);
    }

    public function updatePwd($info)
    {
        //var_dump($info);
        $password = md5($info['password']);
        $sql = "update `user` set  `password`='{$password}' WHERE `user_id`='{$info['user_id']}'";
       
        return $this->db->query($sql);
    }

    

}
